package com.ts.host;

import android.graphics.Typeface;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;


import com.rio.core.U;
import com.rio.helper.sql.SimpleTable;
import com.rio.layout.PopupManager;
import com.rio.layout.TaskManager;
import com.rio.layout.utils.DelayTask;
import com.rio.layout.view.SimpleProgress;
import com.rio.layout.view.SimpleTask;



public class TProgress extends SimpleProgress {

	private static TProgress mInstance;

	private View mContentView;
	
	
	private static boolean forbidGoback;

	private static boolean isShow = false;
	
	private TextView mTextView;
	
	private static final String DEFAULT_TEXT = "正在加载中...";
	

	@Override
	public View onAttach(LayoutInflater layout) {
		if (U.isNull(mContentView)) {
			mContentView = layout.inflate(R.layout.progress, null);
			mTextView = U.findViewById(mContentView, R.id.text);		
		}
		return mContentView;
	}
	
	
	public void setText(String text){
		if(U.notNull(mTextView)){
			mTextView.setText(text);
		}
		
	}

	public static void setForbidGoback(){
		forbidGoback = true;
	}

	@Override
	public boolean onGoBack() {

		return forbidGoback;
	}
	
	public static void show(final String message) {
		if (!isShow) {
			isShow = true;
			TaskManager.getInstance().async(new SimpleTask() {

				@Override
				public void onUIThread(Object data, Object... params)
						throws Exception {
					PopupManager.getInstance().show(getInstance());
					getInstance().setText(message);
					super.onUIThread(data, params);
				}
				
			});
		}
	}

	public static void show() {
		if (!isShow) {
			isShow = true;
			TaskManager.getInstance().async(new SimpleTask() {

				@Override
				public void onUIThread(Object data, Object... params)
						throws Exception {

					PopupManager.getInstance().show(getInstance());
					super.onUIThread(data, params);
				}
				
			});
		}

	}
	
	

	public static void hide() {
		if (isShow) {	
			isShow = false;
			forbidGoback = false;
			TaskManager.getInstance().async(new SimpleTask() {

				@Override
				public void onUIThread(Object data, Object... params)
						throws Exception {
					PopupManager.getInstance().hideProgress();
					getInstance().setText(DEFAULT_TEXT);
				}
			});
		}
	}
 

	private static TProgress getInstance() {
		if (U.isNull(mInstance)) {
			mInstance = new TProgress();
		}
		return mInstance;
	}

}
